package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.domain.CarrierBind;
import com.foreverwin.jzymes.common.enums.CarrierBindEnum;
import com.foreverwin.jzymes.common.mapper.CarrierBindAdviceMapper;
import com.foreverwin.jzymes.common.mapper.CarrierBindMapper;
import com.foreverwin.jzymes.common.service.ICarrierBindAdviceService;
import com.foreverwin.jzymes.common.service.ICommonService;
import com.foreverwin.jzymes.common.service.IItemGroupAdviceService;
import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.sfc.mapper.SFCMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CarrierBindAdviceServiceImpl implements ICarrierBindAdviceService {

    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    ICommonService commonService;
    @Autowired
    CarrierBindAdviceMapper carrierBindAdviceMapper;
    @Autowired
    CarrierBindMapper carrierBindMapper;
    @Autowired
    SFCMapper sfcMapper;


    /**
     * 检查本体绑定状态
     *
     * @param site
     * @param carrier
     * @return
     */
    public List<CarrierBind> existCarrierBindonBind( String site, String carrier ){
        List<CarrierBind> carrierBindList = findCarrierBindOnBind( site, carrier );
        Assert.isTrue( carrierBindList != null && carrierBindList.size() > 0, I18nUtil.getI18nText( "common.carrierNotBind", new String[]{ carrier } ));
        return carrierBindList;
    }

    /**
     * 查询本体绑定中信息
     *
     * @param site
     * @param carrier
     * @return
     */
    public  List<CarrierBind> findCarrierBindOnBind(String site, String carrier ){
        /**检查站点*/
        siteAdviceService.existSite( site );
        /**检查本体编号*/
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ));

        List<CarrierBind> carrierBindList = carrierBindAdviceMapper.selectCarrierBindOnBind( site, carrier );

        return carrierBindList;
    }

    /**
     * 查询物料绑定本体信息
     *
     * @param site
     * @param carrier
     * @param itemBo
     * @return
     */
    public CarrierBind findCarrierBindOnBindByItem( String site, String carrier, String itemBo ){
        /**检查站点*/
        siteAdviceService.existSite( site );
        /**检查物料*/
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        /**检查本体编号*/
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ));

        List<CarrierBind> carrierBindList = findCarrierBindOnBind( site, carrier );
        for( CarrierBind carrierBind : carrierBindList ){
            if( carrierBind.getItemBo().equals( itemBo ) ){
                return carrierBind;
            }
        }
        Assert.isTrue( false, I18nUtil.getI18nText( "common.itemNotBindCarrier", new String[]{ itemBo, carrier } ) );
        return null;
    }

    /**
     * 本体解绑
     *
     * @param carrierBindList
     */
    public void carrierUnBind( List<CarrierBind> carrierBindList, Date endDateTime ){
        /**检查解绑时间*/
        Assert.notNull( endDateTime, I18nUtil.getI18nText( "common.dateNotNull" ) );

        /**查询不到未解绑的本体，返回不做处理*/
        if( carrierBindList == null || carrierBindList.size() == 0 )return;
        for( CarrierBind carrierBind : carrierBindList ){
            carrierUnBind( carrierBind, endDateTime );
        }
    }

    /**
     * 本体解绑
     *
     * @param carrierBind
     * @param endDateTime
     */
    public void carrierUnBind( CarrierBind carrierBind, Date endDateTime ){
        /**检查绑定信息*/
        Assert.notNull( carrierBind, "common.carrierBindNotNull" );
        /**检查解绑时间*/
        Assert.notNull( endDateTime, I18nUtil.getI18nText( "common.dateNotNull" ) );

        carrierBind.setStatus( CarrierBindEnum.UNBIND.getValue() );
        carrierBind.setEndDateTime( endDateTime );
        carrierBindMapper.updateCarrierBind( carrierBind );
    }

    /**
     * 本体解绑
     *
     * @param site
     * @param carrier
     * @param endDateTime
     * @return
     */
    public void carrierUnBind( String site, String carrier, Date endDateTime ){
        /**检查站点*/
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        /**检查本体编号*/
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ) );
        /**检查解绑时间*/
        Assert.notNull( endDateTime, I18nUtil.getI18nText( "common.dateNotNull" ) );

        List<CarrierBind> carrierBindList = findCarrierBindOnBind( site, carrier );
        /**查询不到未解绑的本体，返回不做处理*/
        if( carrierBindList == null || carrierBindList.size() == 0 )return;
        carrierUnBind( carrierBindList, endDateTime );
    }


    /**
     * 物料本体解绑
     *
     * @param site
     * @param carrier
     * @param itemBo
     * @param endDateTime
     */
    public void carrierUnBind( String site, String carrier, String itemBo, Date endDateTime ){
        /**检查站点*/
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        /**检查本体编号*/
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ) );
        /**检查物料*/
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        /**检查解绑时间*/
        Assert.notNull( endDateTime, I18nUtil.getI18nText( "common.dateNotNull" ) );

        CarrierBind carrierBind = findCarrierBindOnBindByItem( site, carrier, itemBo );
        carrierUnBind( carrierBind, endDateTime );
    }

    /**
     * 本体绑定机种物料工令，修改工令已下达数量
     *
     * @param site
     * @param carrier
     * @param bindDate
     */
    public synchronized void carrierBind( String site, String carrier, Date bindDate ){
        /**获取机种*/
        String itemGroup =  itemGroupAdviceService.findItemGroupBySite( site );
        /**获取机种物料*/
        List<Map<String,Object>> items = itemGroupAdviceService.findItemGroupMember( site, itemGroup );
        Assert.notEmpty( items, I18nUtil.getI18nText( "common.itemGroupMember", new String[]{ itemGroup } ) );

        for( Map<String,Object> item : items ){

            /**保存绑定信息*/
            CarrierBind carrierBind = new CarrierBind();
            carrierBind.setSite( site );
            carrierBind.setCarrier( carrier );
            carrierBind.setMachineType( itemGroup );
            carrierBind.setItemBo((String) item.get( "ITEM_BO" ));
            carrierBind.setStatus( CarrierBindEnum.BIND.getValue() );
            carrierBind.setStartDateTime( bindDate );
            carrierBind.setHandle(BOHandleUtil.buildCarrierBindHandle( site, carrier, carrierBind.getItemBo(), bindDate ));
            carrierBindMapper.insertCarrierBind( carrierBind );
        }


    }

    /**
     * 查询本体最近解绑记录
     *
     * @param site
     * @param carrier
     * @return
     */
    public List<CarrierBind> findCarrierUnbind( String site, String carrier ){
        /**获取机种*/
        String itemGroup =  itemGroupAdviceService.findItemGroupBySite( site );
        /**获取机种物料*/
        List<Map<String,Object>> items = itemGroupAdviceService.findItemGroupMember( site, itemGroup );
        Assert.notEmpty( items, I18nUtil.getI18nText( "common.itemGroupMember", new String[]{ itemGroup } ) );

        List<CarrierBind> carrierBindList = new ArrayList<>();
        for( Map<String,Object> item : items ){
            CarrierBind carrierBind = carrierBindAdviceMapper.selectCarrierUnBind( site, carrier, (String) item.get( "ITEM_BO" ) );
            carrierBindList.add( carrierBind );
        }
        return carrierBindList;
    }
}
